<?php
class Admincp_CategoryController extends Main_Controller_Action
{
    private $_arrParam;
    private $_model;
    private $_locale;

    public function init()
    {
        $this->loadTemplates('admin');
        $this->view->headTitle('Quản lý danh mục');
        $this->_arrParam = $this->_request->getParams();
        $this->_model = new Admincp_Model_Category();
        $this->_locale = 'vi';
    }

    public function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if(!$auth->hasIdentity()) {
            if($this->_request->getActionName() != 'login')
                $this->_redirect('admincp/index/login');
        }
    }

    public function indexAction()
    {
        if($_POST['add']) {
            $this->_redirect('admincp/category/create');
        }
        if($_POST['delete']) {
            $listId = $this->_arrParam['check'];
            $this->_model->deleteMultiCategory($listId);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }

        $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listCategory($this->_locale));
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(40);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data = $paginator;
        $this->view->total = $paginator->getTotalItemCount();
    }

    public function createAction()
    {
        $language = new Admincp_Model_Language();
        $this->view->language = $language->listLanguageFull();

        if($this->_request->isPost()) {
            $strTrim = new Zend_Filter_StringTrim();
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $name['vi'] = $strTrim->filter(strip_tags($this->_arrParam['name-vi']));
            $name['zh'] = $strTrim->filter(strip_tags($this->_arrParam['name-zh']));
            $name['en'] = $strTrim->filter(strip_tags($this->_arrParam['name-en']));
            $name['trv'] = $strTrim->filter(strip_tags($this->_arrParam['name-trv']));
            $data_category = array(
                'status'=>1,
            );
            $category_id = $this->_model->addCategory($data_category);
            foreach($name as $key=>$value) {
                foreach($this->view->language as $lang) {
                    if($key == $lang['locale']) {
                        $data = array(
                            'category_id'=>$category_id,
                            'language_id'=>$lang['id'],
                            'name'=>$value,
                            'name_plaintext'=>$strFlex->filter(strtolower($value)),
                        );
                        $this->_model->addCategoryLanguage($data);
                        break;
                    }
                }
            }
            $this->_redirect('admincp/category');
        }
    }

    public function editAction()
    {
        $id = (int) $this->_arrParam['id'];
        $this->view->data = $this->_model->getCategoryById($id);
        $this->view->id = $id;
        $language = new Admincp_Model_Language();
        $this->view->language = $language->listLanguageFull();

        if($this->_request->isPost()) {
            $strTrim = new Zend_Filter_StringTrim();
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $name['vi'] = $strTrim->filter(strip_tags($this->_arrParam['name-vi']));
            $name['zh'] = $strTrim->filter(strip_tags($this->_arrParam['name-zh']));
            $name['en'] = $strTrim->filter(strip_tags($this->_arrParam['name-en']));
            $name['trv'] = $strTrim->filter(strip_tags($this->_arrParam['name-trv']));

            foreach($name as $key=>$value) {
                foreach($this->view->language as $lang) {
                    if($key == $lang['locale']) {
                        $data = array(
                            'language_id'=>$lang['id'],
                            'name'=>$value,
                            'name_plaintext'=>$strFlex->filter(strtolower($value)),
                        );
                        $this->_model->updateCategoryLanguage($data, $id);
                        break;
                    }
                }
            }
            $this->_redirect('admincp/category');
        }
    }

    public function deleteAction()
    {
        $id = (int) $this->_arrParam['id'];
        $this->_model->deleteCategory($id);
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }

    public function publishAction()
    {
        $id = (int) $this->_arrParam['id'];
        $status = (int) $this->_arrParam['status'];
        $status = ($status == 1) ? 2 : 1;
        $data = array(
            'status'=>$status,
        );
        $this->_model->updateCategory($data, $id);
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }

    public function searchAction()
    {
        $strTrim = new Zend_Filter_StringTrim();
        $strFlex = new Zendvn_Filter_RemoveCircumflex();
        $name = $strTrim->filter(strip_tags($this->_arrParam['name']));
        $status = (int) $this->_arrParam['status'];
        $this->view->keyword = array(
            'name'=>$name,
            'status'=>$status,
        );
        $data = array(
            'name'=>(string) '%'.str_replace(' ','%',$strFlex->filter($name)).'%',
            'status'=>$status,
        );
        $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchCategory($data, $this->_locale));
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(40);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data = $paginator;
        $this->view->total = $paginator->getTotalItemCount();
    }

    public function checkAction()
    {
        $this->_helper->layout()->disableLayout();
        $id = (int) $this->_arrParam['id'];
        $name = $this->_arrParam['name'];
        if($this->_model->checkCategory($name, $id) > 0) {
            echo 'Tên danh mục đã tồn tại';
        }
        else {
            echo '';
        }
    }
}
